package com.zy.socket.testfileUpcase;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author Gong
 * @Description
 * @dATE 2021/12/19 14:44
 **/
public class Server {
    public static void main(String[] args) {
        InputStream inputStream =null;
        OutputStream outputStream =null;
        try {
            ServerSocket serverSocket = new ServerSocket(9999);
            //获得客户端对象
            Socket socket = serverSocket.accept();
            System.out.println(socket);

            //输出给客户端数据
             outputStream = socket.getOutputStream();
            //等客户端数据
             inputStream = socket.getInputStream();
            byte[] buff = new byte[1024];
            int len;
            //2.发送图片
            while ((len=inputStream.read(buff)) > -1){
               //将字节转字符串
                String str = new String(buff,0,len);
                //转大写
                str = str.toUpperCase();
                //将转换大写后的数据告知客户端
                outputStream.write(str.getBytes());
            }
            socket.shutdownOutput();
            System.out.println("服务端输出完毕。");

//            OutputStream outputStream = socket.getOutputStream();
//            FileInputStream fileInputStream = new FileInputStream(new File("d:/temp/java.png"));
//            //1.读图片
//            byte[] buff = new byte[1024];
//            int len;
//            //2.发送图片
//            while ((len=fileInputStream.read(buff)) > -1){
//                outputStream.write(buff,0,len);
//            }
//            //告知客户端文件输出完毕
//            socket.shutdownOutput();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
                outputStream.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
